package guda.house.biz.impl;

import guda.grape.autogen.common.page.BaseQuery;
import guda.grape.autogen.common.page.BizResult;
import guda.grape.util.Convert;
import guda.house.biz.BillPayBiz;
import guda.house.biz.beans.vo.*;
import guda.house.biz.constants.CommonResultCode;
import guda.house.biz.enums.*;
import guda.house.biz.form.ReverseForm;
import guda.house.biz.form.bill.BatchNoticeBillForm;
import guda.house.biz.form.bill.BillPaySearchForm;
import guda.house.biz.form.bill.ConfirmPayForm;
import guda.house.biz.form.bill.UnpaySearchForm;
import guda.house.biz.query.*;
import guda.house.biz.security.AuthUser;
import guda.house.biz.util.PriceUtil;
import guda.house.biz.util.Transform;
import guda.house.biz.util.TransformHelper;
import guda.house.dal.dao.*;
import guda.house.dal.dataobject.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class BillPayBizImpl implements BillPayBiz {

    private final static Logger logger = LoggerFactory.getLogger(BillPayBizImpl.class);

    @Autowired
    private BillPayDAO billPayDAO;
    @Autowired
    private SuiteDAO suiteDAO;
    @Autowired
    private UnitDAO unitDAO;
    @Autowired
    private BuildingDAO buildingDAO;
    @Autowired
    private BillParkingDAO billParkingDAO;
    @Autowired
    private BillExtDAO billExtDAO;
    @Autowired
    private BillElecDAO billElecDAO;
    @Autowired
    private BillWaterDAO billWaterDAO;
    @Autowired
    private BillEstateDAO billEstateDAO;
    @Autowired
    private SysVarDAO sysVarDAO;
    @Autowired
    private BankChargeRuleDAO bankChargeRuleDAO;
    @Autowired
    private TransactionTemplate transactionTemplate;
    @Autowired
    private ParkingDAO parkingDAO;
    @Autowired
    private ParkingRentRuleDAO parkingRentRuleDAO;
    @Autowired
    private ParkingRuleDAO parkingRuleDAO;
    @Autowired
    private EstateRuleDAO estateRuleDAO;
    @Autowired
    private WaterRuleDAO waterRuleDAO;
    @Autowired
    private ElecRuleDAO elecRuleDAO;
    @Autowired
    private OwnerDAO ownerDAO;
    @Autowired
    private VillageDAO villageDAO;
    @Autowired
    private InvoiceLogDAO invoiceLogDAO;


    public BizResult detail(long id) {
        BizResult bizResult = new BizResult();
        try {
            BillPayDO billPayDO = billPayDAO.getBillPayById(id);
            bizResult.data.put("billPayDO", billPayDO);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("query BillPay error", e);
        }
        return bizResult;
    }

    public BizResult list(BaseQuery baseQuery) {
        BizResult bizResult = new BizResult();
        try {
            int totalCount = billPayDAO.countForPage(baseQuery);
            baseQuery.setTotalCount(totalCount);
            List<BillPayDO> billPayList = billPayDAO.selectForPage(baseQuery);
            bizResult.data.put("billPayList", billPayList);
            bizResult.data.put("query", baseQuery);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("view BillPay list error", e);
        }
        return bizResult;
    }

    public BizResult delete(long id) {
        BizResult bizResult = new BizResult();
        try {
            billPayDAO.delBillPayById(id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("delete billPay error", e);
        }
        return bizResult;
    }

    public BizResult create(BillPayDO billPayDO) {
        BizResult bizResult = new BizResult();
        try {
            long id = billPayDAO.insert(billPayDO);
            bizResult.data.put("id", id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("create BillPay error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult update(BillPayDO billPayDO) {
        BizResult bizResult = new BizResult();
        try {
            billPayDAO.updateByIdSelective(billPayDO);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("update BillPay error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult listForPayLog(BillPayQuery billPayQuery) {
        BizResult bizResult = new BizResult();
        try {
            if (billPayQuery.getSuiteId() != null) {
                List<BillPayDO> billPayList = billPayDAO.selectForPayedLog(billPayQuery);
                int totalCount = billPayDAO.countForPayedLog(billPayQuery);
                billPayQuery.setTotalCount(totalCount);
                bizResult.data.put("billPayList", billPayList);
                bizResult.data.put("query", billPayQuery);
                bizResult.success = true;
                return bizResult;
            } else if (billPayQuery.getUnitId() != null) {
                SuiteQuery suiteQuery = new SuiteQuery();
                suiteQuery.setUnitId(billPayQuery.getUnitId());
                List<SuiteDO> suiteDOs = suiteDAO.selectForPage(suiteQuery);
                List<Long> suiteIdList = TransformHelper.transform(suiteDOs, new Transform<SuiteDO, Long>() {
                    @Override
                    public Long transform(SuiteDO object) {
                        return object.getSuiteId();
                    }
                });
                billPayQuery.setSuiteIdList(suiteIdList);
                List<BillPayDO> billPayList = billPayDAO.selectForPayedLog(billPayQuery);
                int totalCount = billPayDAO.countForPayedLog(billPayQuery);
                billPayQuery.setTotalCount(totalCount);
                bizResult.data.put("billPayList", billPayList);
                bizResult.data.put("query", billPayQuery);
                bizResult.success = true;
                return bizResult;

            } else if (billPayQuery.getBuildingId() != null) {
                UnitQuery unitQuery = new UnitQuery();
                unitQuery.setBuildingId(billPayQuery.getBuildingId());
                List<UnitDO> unitDOs = unitDAO.selectForPage(unitQuery);
                List<Long> unitIdList = TransformHelper.transform(unitDOs, new Transform<UnitDO, Long>() {
                    @Override
                    public Long transform(UnitDO object) {
                        return object.getUnitId();
                    }
                });

                SuiteQuery suiteQuery = new SuiteQuery();
                suiteQuery.setUnitIdList(unitIdList);
                List<SuiteDO> suiteDOs = suiteDAO.selectForPage(suiteQuery);
                List<Long> suiteIdList = TransformHelper.transform(suiteDOs, new Transform<SuiteDO, Long>() {
                    @Override
                    public Long transform(SuiteDO object) {
                        return object.getSuiteId();
                    }
                });
                billPayQuery.setSuiteIdList(suiteIdList);
                List<BillPayDO> billPayList = billPayDAO.selectForPayedLog(billPayQuery);
                int totalCount = billPayDAO.countForPayedLog(billPayQuery);
                billPayQuery.setTotalCount(totalCount);
                bizResult.data.put("billPayList", billPayList);
                bizResult.data.put("query", billPayQuery);
                bizResult.success = true;
                return bizResult;
            } else if (billPayQuery.getVillageId() != null) {
                BuildingQuery buildingQuery = new BuildingQuery();
                buildingQuery.setPageSize(10000);
                buildingQuery.setVillageId(billPayQuery.getVillageId());
                List<BuildingDO> buildingDOs = buildingDAO.selectForPage(buildingQuery);
                List<Long> buildingIdList = TransformHelper.transform(buildingDOs, new Transform<BuildingDO, Long>() {
                    @Override
                    public Long transform(BuildingDO object) {
                        return object.getBuildingId();
                    }
                });

                UnitQuery unitQuery = new UnitQuery();
                unitQuery.setPageSize(10000);
                unitQuery.setBuildingIdList(buildingIdList);
                List<UnitDO> unitDOs = unitDAO.selectForPage(unitQuery);
                List<Long> unitIdList = TransformHelper.transform(unitDOs, new Transform<UnitDO, Long>() {
                    @Override
                    public Long transform(UnitDO object) {
                        return object.getUnitId();
                    }
                });

                SuiteQuery suiteQuery = new SuiteQuery();
                suiteQuery.setPageSize(10000);
                suiteQuery.setUnitIdList(unitIdList);
                List<SuiteDO> suiteDOs = suiteDAO.selectForPage(suiteQuery);
                List<Long> suiteIdList = TransformHelper.transform(suiteDOs, new Transform<SuiteDO, Long>() {
                    @Override
                    public Long transform(SuiteDO object) {
                        return object.getSuiteId();
                    }
                });
                billPayQuery.setSuiteIdList(suiteIdList);
                List<BillPayDO> billPayList = billPayDAO.selectForPayedLog(billPayQuery);
                int totalCount = billPayDAO.countForPayedLog(billPayQuery);
                billPayQuery.setTotalCount(totalCount);
                bizResult.data.put("billPayList", billPayList);
                bizResult.data.put("query", billPayQuery);
                bizResult.success = true;
                return bizResult;
            }
            List<BillPayDO> billPayList = billPayDAO.selectForPayedLog(billPayQuery);
            int totalCount = billPayDAO.countForPayedLog(billPayQuery);
            billPayQuery.setTotalCount(totalCount);
            bizResult.data.put("billPayList", billPayList);
            bizResult.data.put("query", billPayQuery);
            bizResult.success = true;
            return bizResult;


        } catch (Exception e) {
            logger.error("view BillPay list error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult initSearchForm(BillPaySearchForm billPaySearchForm) {
        BizResult bizResult = new BizResult();
        if (billPaySearchForm.getVillageId() == null) {
            bizResult.success = true;
            return bizResult;
        }
        BuildingQuery buildingQuery = new BuildingQuery();
        buildingQuery.setPageSize(10000);
        buildingQuery.setVillageId(billPaySearchForm.getVillageId());
        List<BuildingDO> buildingDOs = buildingDAO.selectForPage(buildingQuery);
        bizResult.data.put("buildingList", buildingDOs);
        if (billPaySearchForm.getBuildingId() == null) {
            bizResult.success = true;
            return bizResult;
        }
        UnitQuery unitQuery = new UnitQuery();
        unitQuery.setPageSize(10000);
        unitQuery.setBuildingId(billPaySearchForm.getBuildingId());
        List<UnitDO> unitDOs = unitDAO.selectForPage(unitQuery);
        bizResult.data.put("unitList", unitDOs);

        if (billPaySearchForm.getUnitId() == null) {
            bizResult.success = true;
            return bizResult;
        }

        SuiteQuery suiteQuery = new SuiteQuery();
        suiteQuery.setPageSize(10000);
        suiteQuery.setUnitId(billPaySearchForm.getUnitId());
        List<SuiteDO> suiteDOs = suiteDAO.selectForPage(suiteQuery);
        bizResult.data.put("suiteList", suiteDOs);
        bizResult.success = true;
        return bizResult;
    }

    @Override
    public BizResult listForUnPay(UnpayQuery unpayQuery) {
        BizResult bizResult = new BizResult();
        if (StringUtils.isEmpty(unpayQuery.getFeeType()) && unpayQuery.getSuiteId() != null) {
            BillParkingQuery billParkingQuery = new BillParkingQuery();
            billParkingQuery.setPageSize(100);
            billParkingQuery.setSuiteId(unpayQuery.getSuiteId());
            billParkingQuery.setEndDate(unpayQuery.getEndDate());
            billParkingQuery.setStartDate(unpayQuery.getStartDate());
            List<BillParkingDO> billParkingDOs = billParkingDAO.selectForUnpay(unpayQuery);
            bizResult.data.put("billParkingList", billParkingDOs);

            BillEstateQuery billEstateQuery = new BillEstateQuery();
            billEstateQuery.setSuiteId(unpayQuery.getSuiteId());
            billEstateQuery.setPageSize(100);
            billEstateQuery.setEndDate(unpayQuery.getEndDate());
            billEstateQuery.setStartDate(unpayQuery.getStartDate());
            List<BillEstateDO> billEstateDOList = billEstateDAO.selectForUnpay(billEstateQuery);
            bizResult.data.put("billEstateList", billEstateDOList);

            BillWaterQuery billWaterQuery = new BillWaterQuery();
            billWaterQuery.setSuiteId(unpayQuery.getSuiteId());
            billWaterQuery.setPageSize(100);
            billWaterQuery.setEndDate(unpayQuery.getEndDate());
            billWaterQuery.setStartDate(unpayQuery.getStartDate());
            List<BillWaterDO> billWaterDOList = billWaterDAO.selectForUnpay(billWaterQuery);
            bizResult.data.put("billWaterList", billWaterDOList);

            BillElecQuery billElecQuery = new BillElecQuery();
            billElecQuery.setSuiteId(unpayQuery.getSuiteId());
            billElecQuery.setPageSize(100);
            billElecQuery.setEndDate(unpayQuery.getEndDate());
            billElecQuery.setStartDate(unpayQuery.getStartDate());
            List<BillElecDO> billElecDOList = billElecDAO.selectForUnpay(billElecQuery);
            bizResult.data.put("billElecList", billElecDOList);

            BillExtQuery billExtQuery = new BillExtQuery();
            billExtQuery.setSuiteId(unpayQuery.getSuiteId());
            billExtQuery.setPageSize(100);
            billExtQuery.setEndDate(unpayQuery.getEndDate());
            billExtQuery.setStartDate(unpayQuery.getStartDate());
            List<BillExtDO> billExtDOList = billExtDAO.selectForUnpay(billExtQuery);
            bizResult.data.put("billExtList", billExtDOList);

            SuiteDO suiteById = suiteDAO.getSuiteById(unpayQuery.getSuiteId());
            bizResult.data.put("suiteDO", suiteById);
            bizResult.success = true;
            return bizResult;
        } else {
            //统计suiteId
            List<Long> suiteIdList = getSuiteIdList(unpayQuery);
            if (FeeTypeEnum.deposit.name().equals(unpayQuery.getFeeType())) {
                BillExtQuery billExtQuery = new BillExtQuery();
                if (suiteIdList.size() == 1) {
                    billExtQuery.setSuiteId(suiteIdList.get(0));
                } else {
                    billExtQuery.setSuiteIdList(suiteIdList);
                }
                billExtQuery.setBillCat(BillCatEnum.deposit.name());
                billExtQuery.setEndDate(unpayQuery.getEndDate());
                billExtQuery.setStartDate(unpayQuery.getStartDate());
                List<BillExtDO> billExtDOs = billExtDAO.selectForUnpay(billExtQuery);
                int totalCount = billExtDAO.countForUnpay(billExtQuery);
                unpayQuery.setTotalCount(totalCount);
                bizResult.data.put("billExtList", billExtDOs);
                bizResult.success = true;
                return bizResult;
            } else if (FeeTypeEnum.elec.name().equals(unpayQuery.getFeeType())) {
                BillElecQuery billElecQuery = new BillElecQuery();
                if (suiteIdList.size() == 1) {
                    billElecQuery.setSuiteId(suiteIdList.get(0));
                } else {
                    billElecQuery.setSuiteIdList(suiteIdList);
                }
                billElecQuery.setEndDate(unpayQuery.getEndDate());
                billElecQuery.setStartDate(unpayQuery.getStartDate());
                List<BillElecDO> billElecDOList = billElecDAO.selectForUnpay(billElecQuery);
                int totalCount = billElecDAO.countForUnpay(billElecQuery);
                unpayQuery.setTotalCount(totalCount);
                bizResult.data.put("billElecList", billElecDOList);
                bizResult.success = true;
                return bizResult;
            } else if (FeeTypeEnum.estate.name().equals(unpayQuery.getFeeType())) {
                BillEstateQuery billEstateQuery = new BillEstateQuery();
                if (suiteIdList.size() == 1) {
                    billEstateQuery.setSuiteId(suiteIdList.get(0));
                } else {
                    billEstateQuery.setSuiteIdList(suiteIdList);
                }
                billEstateQuery.setEndDate(unpayQuery.getEndDate());
                billEstateQuery.setStartDate(unpayQuery.getStartDate());
                List<BillEstateDO> billEstateDOList = billEstateDAO.selectForUnpay(billEstateQuery);
                int totalCount = billEstateDAO.countForUnpay(billEstateQuery);
                unpayQuery.setTotalCount(totalCount);
                bizResult.data.put("billEstateList", billEstateDOList);
                bizResult.success = true;
                return bizResult;
            } else if (FeeTypeEnum.parking.name().equals(unpayQuery.getFeeType())) {
                BillParkingQuery billParkingQuery = new BillParkingQuery();
                if (suiteIdList.size() == 1) {
                    billParkingQuery.setSuiteId(suiteIdList.get(0));
                } else {
                    billParkingQuery.setSuiteIdList(suiteIdList);
                }
                billParkingQuery.setEndDate(unpayQuery.getEndDate());
                billParkingQuery.setStartDate(unpayQuery.getStartDate());
                List<BillParkingDO> billParkingDOList = billParkingDAO.selectForUnpay(billParkingQuery);
                int totalCount = billParkingDAO.countForUnpay(billParkingQuery);
                unpayQuery.setTotalCount(totalCount);
                bizResult.data.put("billParkingList", billParkingDOList);
                bizResult.success = true;
                return bizResult;
            } else if (FeeTypeEnum.water.name().equals(unpayQuery.getFeeType())) {
                BillWaterQuery billWaterQuery = new BillWaterQuery();
                if (suiteIdList.size() == 1) {
                    billWaterQuery.setSuiteId(suiteIdList.get(0));
                } else {
                    billWaterQuery.setSuiteIdList(suiteIdList);
                }
                billWaterQuery.setEndDate(unpayQuery.getEndDate());
                billWaterQuery.setStartDate(unpayQuery.getStartDate());
                List<BillWaterDO> billWaterDOList = billWaterDAO.selectForUnpay(billWaterQuery);
                int totalCount = billWaterDAO.countForUnpay(billWaterQuery);
                unpayQuery.setTotalCount(totalCount);
                bizResult.data.put("billWaterList", billWaterDOList);
                bizResult.success = true;
                return bizResult;
            } else if (FeeTypeEnum.other.name().equals(unpayQuery.getFeeType())) {
                BillExtQuery billExtQuery = new BillExtQuery();
                if (suiteIdList.size() == 1) {
                    billExtQuery.setSuiteId(suiteIdList.get(0));
                } else {
                    billExtQuery.setSuiteIdList(suiteIdList);
                }
                billExtQuery.setEndDate(unpayQuery.getEndDate());
                billExtQuery.setStartDate(unpayQuery.getStartDate());
                billExtQuery.setBillCat(BillCatEnum.other.name());
                List<BillExtDO> billExtDOs = billExtDAO.selectForUnpay(billExtQuery);
                int totalCount = billExtDAO.countForUnpay(billExtQuery);
                unpayQuery.setTotalCount(totalCount);
                bizResult.data.put("billExtList", billExtDOs);
                bizResult.success = true;
                return bizResult;
            }
        }
        bizResult.success = true;
        return bizResult;
    }

    private List<Long> getSuiteIdList(UnpayQuery unpayQuery) {
        List<Long> idList = new ArrayList<Long>();
        if(StringUtils.hasText(unpayQuery.getOwnerName())){
            OwnerQuery ownerQuery = new OwnerQuery();
            ownerQuery.setOwnerName(unpayQuery.getOwnerName());
            List<OwnerDO> ownerDOs = ownerDAO.selectForPage(ownerQuery);
            List<Long> id = TransformHelper.transform(ownerDOs, new Transform<OwnerDO, Long>() {
                @Override
                public Long transform(OwnerDO object) {
                    return object.getSuiteId();
                }
            });
            if(id.size()>0){
                idList.addAll(id);
            }
        }
        if(StringUtils.hasText(unpayQuery.getParkingCard()) || StringUtils.hasText(unpayQuery.getParkingName())){
            ParkingQuery parkingQuery = new ParkingQuery();
            if(StringUtils.hasText(unpayQuery.getParkingCard())) {
                parkingQuery.setParkingCard(unpayQuery.getParkingCard());
            }
            if(StringUtils.hasText(unpayQuery.getParkingName())) {
                parkingQuery.setParkingName(parkingQuery.getParkingName());
            }
            List<ParkingDO> parkingDOs = parkingDAO.selectForPage(parkingQuery);
            List<Long> id = TransformHelper.transform(parkingDOs, new Transform<ParkingDO, Long>() {
                @Override
                public Long transform(ParkingDO object) {
                    return object.getSuiteId();
                }
            });
            if(id.size()>0){
                idList.addAll(id);
            }
        }
        List<Long> suiteIdList = new ArrayList<Long>();
        if (unpayQuery.getSuiteId() != null) {
            suiteIdList.add(unpayQuery.getSuiteId());
            return suiteIdList;
        }
        if (unpayQuery.getUnitId() != null) {
            SuiteQuery suiteQuery = new SuiteQuery();
            suiteQuery.setUnitId(unpayQuery.getUnitId());
            suiteQuery.setPageSize(1000);
            if(idList.size()>0){
                suiteQuery.setSuiteIdList(idList);
            }
            List<SuiteDO> suiteDOs = suiteDAO.selectForPage(suiteQuery);
            suiteIdList = TransformHelper.transform(suiteDOs, new Transform<SuiteDO, Long>() {
                @Override
                public Long transform(SuiteDO object) {
                    return object.getSuiteId();
                }
            });
            return suiteIdList;
        }
        if (unpayQuery.getBuildingId() != null) {
            UnitQuery unitQuery = new UnitQuery();
            unitQuery.setPageSize(1000);
            unitQuery.setBuildingId(unpayQuery.getBuildingId());
            List<UnitDO> unitDOs = unitDAO.selectForPage(unitQuery);
            List<Long> unitIdList = TransformHelper.transform(unitDOs, new Transform<UnitDO, Long>() {
                @Override
                public Long transform(UnitDO object) {
                    return object.getUnitId();
                }
            });

            SuiteQuery suiteQuery = new SuiteQuery();
            suiteQuery.setUnitIdList(unitIdList);
            suiteQuery.setPageSize(1000);
            if(idList.size()>0){
                suiteQuery.setSuiteIdList(idList);
            }
            List<SuiteDO> suiteDOs = suiteDAO.selectForPage(suiteQuery);
            suiteIdList = TransformHelper.transform(suiteDOs, new Transform<SuiteDO, Long>() {
                @Override
                public Long transform(SuiteDO object) {
                    return object.getSuiteId();
                }
            });
            return suiteIdList;

        }
        if (unpayQuery.getVillageId() != null) {
            BuildingQuery buildingQuery = new BuildingQuery();
            buildingQuery.setPageSize(1000);
            buildingQuery.setVillageId(unpayQuery.getVillageId());
            List<BuildingDO> buildingDOs = buildingDAO.selectForPage(buildingQuery);
            List<Long> buildingIdList = TransformHelper.transform(buildingDOs, new Transform<BuildingDO, Long>() {
                @Override
                public Long transform(BuildingDO object) {
                    return object.getBuildingId();
                }
            });

            UnitQuery unitQuery = new UnitQuery();
            unitQuery.setPageSize(1000);
            unitQuery.setBuildingIdList(buildingIdList);
            List<UnitDO> unitDOs = unitDAO.selectForPage(unitQuery);
            List<Long> unitIdList = TransformHelper.transform(unitDOs, new Transform<UnitDO, Long>() {
                @Override
                public Long transform(UnitDO object) {
                    return object.getUnitId();
                }
            });

            SuiteQuery suiteQuery = new SuiteQuery();
            suiteQuery.setUnitIdList(unitIdList);
            suiteQuery.setPageSize(1000);
            if(idList.size()>0){
                suiteQuery.setSuiteIdList(idList);
            }
            List<SuiteDO> suiteDOs = suiteDAO.selectForPage(suiteQuery);
            suiteIdList = TransformHelper.transform(suiteDOs, new Transform<SuiteDO, Long>() {
                @Override
                public Long transform(SuiteDO object) {
                    return object.getSuiteId();
                }
            });
            return suiteIdList;
        }

        List<Long> villageIdList = AuthUser.getCurrentUser().getVillageIdList();
        BuildingQuery buildingQuery = new BuildingQuery();
        buildingQuery.setPageSize(1000);
        buildingQuery.setVillageIdList(villageIdList);
        List<BuildingDO> buildingDOs = buildingDAO.selectForPage(buildingQuery);
        List<Long> buildingIdList = TransformHelper.transform(buildingDOs, new Transform<BuildingDO, Long>() {
            @Override
            public Long transform(BuildingDO object) {
                return object.getBuildingId();
            }
        });

        UnitQuery unitQuery = new UnitQuery();
        unitQuery.setPageSize(1000);
        unitQuery.setBuildingIdList(buildingIdList);
        List<UnitDO> unitDOs = unitDAO.selectForPage(unitQuery);
        List<Long> unitIdList = TransformHelper.transform(unitDOs, new Transform<UnitDO, Long>() {
            @Override
            public Long transform(UnitDO object) {
                return object.getUnitId();
            }
        });

        SuiteQuery suiteQuery = new SuiteQuery();
        suiteQuery.setUnitIdList(unitIdList);
        suiteQuery.setPageSize(1000);
        if(idList.size()>0){
            suiteQuery.setSuiteIdList(idList);
        }
        List<SuiteDO> suiteDOs = suiteDAO.selectForPage(suiteQuery);
        suiteIdList = TransformHelper.transform(suiteDOs, new Transform<SuiteDO, Long>() {
            @Override
            public Long transform(SuiteDO object) {
                return object.getSuiteId();
            }
        });
        return suiteIdList;
    }

    @Override
    public BizResult initSearchForm(UnpaySearchForm unpaySearchForm) {
        BizResult bizResult = new BizResult();
        if (unpaySearchForm.getVillageId() == null) {
            bizResult.success = true;
            return bizResult;
        }
        BuildingQuery buildingQuery = new BuildingQuery();
        buildingQuery.setPageSize(10000);
        buildingQuery.setVillageId(unpaySearchForm.getVillageId());
        List<BuildingDO> buildingDOs = buildingDAO.selectForPage(buildingQuery);
        bizResult.data.put("buildingList", buildingDOs);
        if (unpaySearchForm.getBuildingId() == null) {
            bizResult.success = true;
            return bizResult;
        }
        UnitQuery unitQuery = new UnitQuery();
        unitQuery.setPageSize(10000);
        unitQuery.setBuildingId(unpaySearchForm.getBuildingId());
        List<UnitDO> unitDOs = unitDAO.selectForPage(unitQuery);
        bizResult.data.put("unitList", unitDOs);

        if (unpaySearchForm.getUnitId() == null) {
            bizResult.success = true;
            return bizResult;
        }

        SuiteQuery suiteQuery = new SuiteQuery();
        suiteQuery.setPageSize(10000);
        suiteQuery.setUnitId(unpaySearchForm.getUnitId());
        List<SuiteDO> suiteDOs = suiteDAO.selectForPage(suiteQuery);
        bizResult.data.put("suiteList", suiteDOs);


        bizResult.success = true;


        return bizResult;
    }

    @Override
    public BizResult initForConfirmPay(Long villageId) {
        BizResult bizResult = new BizResult();
        try {
            SysVarQuery sysVarQuery = new SysVarQuery();
            sysVarQuery.setVillageId(villageId);
            sysVarQuery.setVarType(SysVarTypeEnum.deposit.name());
            sysVarQuery.setPageSize(500);
            List<SysVarDO> sysVarDOs = sysVarDAO.selectForPage(sysVarQuery);
            bizResult.data.put("depositSysVarList", sysVarDOs);


            sysVarQuery.setVarType(SysVarTypeEnum.ext.name());

            List<SysVarDO> extSysVarDOs = sysVarDAO.selectForPage(sysVarQuery);
            bizResult.data.put("extSysVarList", extSysVarDOs);

            sysVarQuery.setVarType(SysVarTypeEnum.bank_charge.name());

            List<SysVarDO> payTypeSysVarDOs = sysVarDAO.selectForPage(sysVarQuery);
            bizResult.data.put("payTypeSysVarList", payTypeSysVarDOs);

            bizResult.success = true;
        } catch (Exception e) {
            logger.error("query error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult initSearchForm(ConfirmPayForm confirmPayForm) {
        BizResult bizResult = new BizResult();
        if (confirmPayForm.getVillageId() == null) {
            bizResult.success = true;
            return bizResult;
        }

        SysVarQuery sysVarQuery = new SysVarQuery();
        sysVarQuery.setVillageId(confirmPayForm.getVillageId());
        sysVarQuery.setVarType(SysVarTypeEnum.deposit.name());
        sysVarQuery.setPageSize(500);
        List<SysVarDO> sysVarDOs = sysVarDAO.selectForPage(sysVarQuery);
        bizResult.data.put("depositSysVarList", sysVarDOs);


        sysVarQuery.setVarType(SysVarTypeEnum.ext.name());

        List<SysVarDO> extSysVarDOs = sysVarDAO.selectForPage(sysVarQuery);
        bizResult.data.put("extSysVarList", extSysVarDOs);

        sysVarQuery.setVarType(SysVarTypeEnum.bank_charge.name());

        List<SysVarDO> payTypeSysVarDOs = sysVarDAO.selectForPage(sysVarQuery);
        bizResult.data.put("payTypeSysVarList", payTypeSysVarDOs);

        BuildingQuery buildingQuery = new BuildingQuery();
        buildingQuery.setPageSize(10000);
        buildingQuery.setVillageId(confirmPayForm.getVillageId());
        List<BuildingDO> buildingDOs = buildingDAO.selectForPage(buildingQuery);
        bizResult.data.put("buildingList", buildingDOs);
        if (confirmPayForm.getBuildingId() == null) {
            bizResult.success = true;
            return bizResult;
        }
        UnitQuery unitQuery = new UnitQuery();
        unitQuery.setPageSize(10000);
        unitQuery.setBuildingId(confirmPayForm.getBuildingId());
        List<UnitDO> unitDOs = unitDAO.selectForPage(unitQuery);
        bizResult.data.put("unitList", unitDOs);

        if (confirmPayForm.getUnitId() == null) {
            bizResult.success = true;
            return bizResult;
        }

        SuiteQuery suiteQuery = new SuiteQuery();
        suiteQuery.setPageSize(10000);
        suiteQuery.setUnitId(confirmPayForm.getUnitId());
        List<SuiteDO> suiteDOs = suiteDAO.selectForPage(suiteQuery);
        bizResult.data.put("suiteList", suiteDOs);



//        sysVarQuery = new SysVarQuery();
//        sysVarQuery.setVillageId(confirmPayForm.getVillageId());
//        sysVarQuery.setVarType(SysVarTypeEnum.bank_charge.name());
//        sysVarQuery.setPageSize(500);
//        sysVarDOs = sysVarDAO.selectForPage(sysVarQuery);
//        bizResult.data.put("payTypeSysVarList", payTypeSysVarDOs);
        bizResult.success = true;
        return bizResult;
    }

    @Override
    public BizResult pay(final ConfirmPayForm confirmPayForm, final Map<String, String[]> parameterMap) {
        final BizResult bizResult = new BizResult();


        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
                long total = 0;
                StringBuilder buf = new StringBuilder();
                if (confirmPayForm.getBillIdList() != null) {
                    List<String> billIdList = confirmPayForm.getBillIdList();
                    for (String billId : billIdList) {
                        String[] split = billId.split("_");
                        if ("parking".equals(split[0])) {
                            BillParkingDO billParkingById = billParkingDAO.getBillParkingById(Convert.toLong(split[1]));
                            total = total + billParkingById.getPayTotal();
                            buf.append("车位费（").append(billParkingById.getRecordYear())
                                    .append("年").append(billParkingById.getRecordMonth()).append("月）：").append(PriceUtil.formatCNY(billParkingById.getPayTotal())).append("元;");
                        } else if ("estate".equals(split[0])) {
                            BillEstateDO billEstateById = billEstateDAO.getBillEstateById(Convert.toLong(split[1]));
                            total = total + billEstateById.getPayTotal();
                            buf.append("物业费（").append(billEstateById.getRecordYear())
                                    .append("年").append(billEstateById.getRecordMonth()).append("月）：").append(PriceUtil.formatCNY(billEstateById.getPayTotal())).append("元;");
                        } else if ("water".equals(split[0])) {
                            BillWaterDO billWaterById = billWaterDAO.getBillWaterById(Convert.toLong(split[1]));
                            total = total + billWaterById.getPayTotal();
                            buf.append("水费（").append(billWaterById.getRecordYear())
                                    .append("年").append(billWaterById.getRecordMonth()).append("月）：").append(PriceUtil.formatCNY(billWaterById.getPayTotal())).append("元;");
                        } else if ("elec".equals(split[0])) {
                            BillElecDO billElecById = billElecDAO.getBillElecById(Convert.toLong(split[1]));
                            total = total + billElecById.getPayTotal();
                            buf.append("电费（").append(billElecById.getRecordYear())
                                    .append("年").append(billElecById.getRecordMonth()).append("月）：").append(PriceUtil.formatCNY(billElecById.getPayTotal())).append("元;");
                        } else if ("deposit".equals(split[0])) {
                            BillExtDO billExtById = billExtDAO.getBillExtById(Convert.toLong(split[1]));
                            total = total + billExtById.getPayTotal();
                            buf.append("押金:").append(PriceUtil.formatCNY(billExtById.getPayTotal())).append("元;");
                        } else if ("other".equals(split[0])) {
                            BillExtDO billExtById = billExtDAO.getBillExtById(Convert.toLong(split[1]));
                            total = total + billExtById.getPayTotal();
                            buf.append("其他费用:").append(PriceUtil.formatCNY(billExtById.getPayTotal())).append("元;");
                        }
                    }
                }
                if (confirmPayForm.getDepositValList() != null && confirmPayForm.getDepositTypeList() != null) {
                    for (int i = 0, len = confirmPayForm.getDepositValList().size(); i < len; ++i) {
                        String val = confirmPayForm.getDepositValList().get(i);
                        Long typeId = confirmPayForm.getDepositTypeList().get(i);
                        total = total + PriceUtil.convertPriceYuan(val);
                        SysVarDO sysVarById1 = sysVarDAO.getSysVarById(typeId);
                        buf.append(BillCatEnum.deposit.getDesc()).append("(").append(sysVarById1.getVarValue()).append(")").append(val).append("元;");
                    }

                }
                if (confirmPayForm.getOtherValList() != null && confirmPayForm.getOtherTypeList() != null) {
                    for (int i = 0, len = confirmPayForm.getOtherValList().size(); i < len; ++i) {
                        String val = confirmPayForm.getOtherValList().get(i);
                        Long typeId = confirmPayForm.getOtherTypeList().get(i);
                        total = total + PriceUtil.convertPriceYuan(val);
                        SysVarDO sysVarById1 = sysVarDAO.getSysVarById(typeId);
                        buf.append(BillCatEnum.other.getDesc()).append("(").append(sysVarById1.getVarValue()).append(")").append(val).append("元;");
                    }
                }
                if (confirmPayForm.getIcValList() != null && confirmPayForm.getIcTypeList() != null) {
                    for (int i = 0, len = confirmPayForm.getIcValList().size(); i < len; ++i) {
                        String val = confirmPayForm.getIcValList().get(i);
                        String type = confirmPayForm.getIcTypeList().get(i);
                        total = total + PriceUtil.convertPriceYuan(val);
                        if(IcTypeEnum.elec.name().equals(type)){
                            buf.append("(IC卡电费)").append(val).append("元;");
                        }else{
                            buf.append("(IC卡水费)").append(val).append("元;");
                        }

                    }
                }
                BillPayDO billPayDO = new BillPayDO();
                billPayDO.setSuiteId(confirmPayForm.getSuiteId());
                billPayDO.setPayTypeId(confirmPayForm.getPayWayId());
                billPayDO.setPayComment(confirmPayForm.getComment());
                billPayDO.setPayTotal(total);
                billPayDO.setPayDesc(buf.toString());
                //计算手续费
                BankChargeRuleQuery bankChargeRuleQuery = new BankChargeRuleQuery();
                bankChargeRuleQuery.setVillageId(confirmPayForm.getVillageId());
                bankChargeRuleQuery.setVarId(confirmPayForm.getPayWayId());
                List<BankChargeRuleDO> bankChargeRuleDOs = bankChargeRuleDAO.selectForPage(bankChargeRuleQuery);
                BankChargeRuleDO bankChargeRuleDO = bankChargeRuleDOs.get(0);

                if (total > bankChargeRuleDO.getMaxValue()) {
                    billPayDO.setPayTotalCharge(Long.valueOf(bankChargeRuleDO.getMaxFee()));
                } else {
                    billPayDO.setPayTotalCharge(total * bankChargeRuleDO.getChargeUnit() / 10000);
                }
                SysVarDO sysVarById = sysVarDAO.getSysVarById(confirmPayForm.getPayWayId());
                billPayDO.setPayType(sysVarById.getVarValue());
                Long payId = billPayDAO.insert(billPayDO);
                bizResult.data.put("payId",payId);
                long discountTotal = 0;
                if (confirmPayForm.getBillIdList() != null) {
                    List<String> billIdList = confirmPayForm.getBillIdList();
                    for (String billId : billIdList) {
                        String[] split = billId.split("_");
                        if ("parking".equals(split[0])) {
                            BillParkingDO billParkingById = billParkingDAO.getBillParkingById(Convert.toLong(split[1]));
                            billParkingById.setIsPayed(1);
                            billParkingById.setGmtPay(new Date());
                            billParkingById.setBillPayId(payId);

                            long parkingTotal = billParkingById.getPayTotal();
                            String[] dichan = parameterMap.get("parking_dichan_" + split[1]);
                            if (dichan != null && dichan.length > 0&& StringUtils.hasText(dichan[0])) {
                                long dis = Long.parseLong(dichan[0]) * 100;
                                billParkingById.setDiscountEstate(dis);
                                parkingTotal=parkingTotal-dis;
                                discountTotal +=dis;
                            }
                            String[] self = parameterMap.get("parking_self_" + split[1]);
                            if (self != null && self.length > 0&& StringUtils.hasText(self[0])) {
                                long dis = Long.parseLong(self[0]) * 100;
                                billParkingById.setDiscountSelf(dis);
                                parkingTotal=parkingTotal-dis;
                                discountTotal +=dis;
                            }
                            String[] discount = parameterMap.get("parking_discount_" + split[1]);
                            if (discount != null && discount.length > 0&& StringUtils.hasText(discount[0])) {
                                long dis = Long.parseLong(discount[0]) * 1000;
                                billParkingById.setDiscount(dis);
                                long fee =  parkingTotal*dis/10000;
                                discountTotal+=(parkingTotal - fee);
                                parkingTotal=fee;
                            }
                            billParkingById.setPayTotal(parkingTotal);
                            billParkingDAO.updateByIdSelective(billParkingById);
                        } else if ("estate".equals(split[0])) {
                            BillEstateDO billEstateById = billEstateDAO.getBillEstateById(Convert.toLong(split[1]));
                            billEstateById.setIsPayed(1);
                            billEstateById.setGmtPay(new Date());
                            billEstateById.setBillPayId(payId);
                            long estateTotal = billEstateById.getPayTotal();
                            String[] dichan = parameterMap.get("estate_dichan_" + split[1]);
                            if (dichan != null && dichan.length > 0&& StringUtils.hasText(dichan[0])) {
                                long dis = Long.parseLong(dichan[0]) * 100;
                                billEstateById.setDiscountEstate(dis);
                                discountTotal +=dis;
                                estateTotal=estateTotal-dis;
                            }
                            String[] self = parameterMap.get("estate_self_" + split[1]);
                            if (self != null && self.length > 0&& StringUtils.hasText(self[0])) {
                                long dis = Long.parseLong(self[0]) * 100;
                                billEstateById.setDiscountSelf(dis);
                                discountTotal +=dis;
                                estateTotal=estateTotal-dis;
                            }
                            String[] discount = parameterMap.get("estate_discount_" + split[1]);
                            if (discount != null && discount.length > 0&& StringUtils.hasText(discount[0])) {
                                long dis = Long.parseLong(discount[0]) * 1000;
                                billEstateById.setDiscount(dis);
                                long fee =  estateTotal*dis/10000;
                                discountTotal+=(estateTotal - fee);
                                estateTotal=fee;
                            }
                            billEstateById.setPayTotal(estateTotal);
                            billEstateDAO.updateByIdSelective(billEstateById);
                        } else if ("water".equals(split[0])) {

                            BillWaterDO billWaterById = billWaterDAO.getBillWaterById(Convert.toLong(split[1]));
                            billWaterById.setIsPayed(1);
                            billWaterById.setGmtPay(new Date());
                            billWaterById.setBillPayId(payId);
                            long waterTotal = billWaterById.getPayTotal();
                            String[] dichan = parameterMap.get("water_dichan_" + split[1]);
                            if (dichan != null && dichan.length > 0 && StringUtils.hasText(dichan[0])) {
                                long dis = Long.parseLong(dichan[0]) * 100;
                                billWaterById.setDiscountEstate(dis);
                                discountTotal +=dis;
                                waterTotal=waterTotal-dis;
                            }
                            String[] self = parameterMap.get("water_self_" + split[1]);
                            if (self != null && self.length > 0&& StringUtils.hasText(self[0])) {
                                long dis = Long.parseLong(self[0]) * 100;
                                billWaterById.setDiscountSelf(dis);
                                discountTotal +=dis;
                                waterTotal=waterTotal-dis;
                            }
                            String[] discount = parameterMap.get("water_discount_" + split[1]);
                            if (discount != null && discount.length > 0&& StringUtils.hasText(discount[0])) {
                                long dis = Long.parseLong(discount[0]) * 1000;
                                billWaterById.setDiscount(dis);
                                long fee =  waterTotal*dis/10000;
                                waterTotal=waterTotal-dis;
                                discountTotal+=(waterTotal - fee);
                                waterTotal=fee;
                            }
                            billWaterById.setPayTotal(waterTotal);
                            billWaterDAO.updateByIdSelective(billWaterById);
                        } else if ("elec".equals(split[0])) {
                            BillElecDO billElecById = billElecDAO.getBillElecById(Convert.toLong(split[1]));
                            billElecById.setIsPayed(1);
                            billElecById.setGmtPay(new Date());
                            billElecById.setBillPayId(payId);
                            long elecTotal = billElecById.getPayTotal();
                            String[] dichan = parameterMap.get("elec_dichan_" + split[1]);
                            if (dichan != null && dichan.length > 0&& StringUtils.hasText(dichan[0])) {
                                long dis = Long.parseLong(dichan[0]) * 100;
                                billElecById.setDiscountEstate(dis);
                                discountTotal +=dis;
                            }
                            String[] self = parameterMap.get("elec_self_" + split[1]);
                            if (self != null && self.length > 0&& StringUtils.hasText(self[0])) {
                                long dis = Long.parseLong(self[0]) * 100;
                                billElecById.setDiscountSelf(dis);
                                discountTotal +=dis;
                            }
                            String[] discount = parameterMap.get("elec_discount_" + split[1]);
                            if (discount != null && discount.length > 0&& StringUtils.hasText(discount[0])) {
                                long dis = Long.parseLong(discount[0]) * 1000;
                                billElecById.setDiscount(dis);
                                long fee =  elecTotal*dis/10000;
                                discountTotal+=(elecTotal - fee);
                                elecTotal=fee;
                            }
                            billElecById.setPayTotal(elecTotal);
                            billElecDAO.updateByIdSelective(billElecById);
                        } else if ("deposit".equals(split[0])) {
                            BillExtDO billExtById = billExtDAO.getBillExtById(Convert.toLong(split[1]));
                            billExtById.setIsPayed(1);
                            billExtById.setBillPayId(payId);
                            long extTotal = billExtById.getPayTotal();
                            String[] dichan = parameterMap.get("deposit_dichan_" + split[1]);
                            if (dichan != null && dichan.length > 0&& StringUtils.hasText(dichan[0])) {
                                long dis = Long.parseLong(dichan[0]) * 100;
                                billExtById.setDiscountEstate(dis);
                                discountTotal +=dis;
                            }
                            String[] self = parameterMap.get("deposit_self_" + split[1]);
                            if (self != null && self.length > 0&& StringUtils.hasText(self[0])) {
                                long dis = Long.parseLong(self[0]) * 100;
                                billExtById.setDiscountSelf(dis);
                                discountTotal +=dis;
                            }
                            String[] discount = parameterMap.get("deposit_discount_" + split[1]);
                            if (discount != null && discount.length > 0&& StringUtils.hasText(discount[0])) {
                                long dis = Long.parseLong(discount[0]) * 1000;
                                billExtById.setDiscount(dis);
                                long fee =  extTotal*dis/10000;
                                discountTotal+=(extTotal - fee);
                                extTotal=fee;
                            }
                            billExtById.setPayTotal(extTotal);
                            billExtDAO.updateByIdSelective(billExtById);
                        } else if ("other".equals(split[0])) {
                            BillExtDO billExtById = billExtDAO.getBillExtById(Convert.toLong(split[1]));
                            billExtById.setIsPayed(1);
                            billExtById.setBillPayId(payId);
                            long otherTotal = billExtById.getPayTotal();
                            String[] dichan = parameterMap.get("other_dichan_" + split[1]);
                            if (dichan != null && dichan.length > 0&& StringUtils.hasText(dichan[0])) {
                                long dis = Long.parseLong(dichan[0]) * 100;
                                billExtById.setDiscountEstate(dis);
                                discountTotal +=dis;
                            }
                            String[] self = parameterMap.get("other_self_" + split[1]);
                            if (self != null && self.length > 0&& StringUtils.hasText(self[0])) {
                                long dis = Long.parseLong(self[0]) * 100;
                                billExtById.setDiscountSelf(dis);
                                discountTotal +=dis;
                            }
                            String[] discount = parameterMap.get("other_discount_" + split[1]);
                            if (discount != null && discount.length > 0&& StringUtils.hasText(discount[0])) {
                                long dis = Long.parseLong(discount[0]) * 1000;
                                billExtById.setDiscount(dis);
                                long fee =  otherTotal*dis/10000;
                                discountTotal+=(otherTotal - fee);
                                otherTotal=fee;
                            }
                            billExtById.setPayTotal(otherTotal);
                            billExtDAO.updateByIdSelective(billExtById);
                        }
                    }
                }
                if(discountTotal >0){
                    billPayDO.setPayId(payId);
                    total = billPayDO.getPayTotal() - discountTotal;
                    billPayDO.setPayTotal( total);
                    if (total > bankChargeRuleDO.getMaxValue()) {
                        billPayDO.setPayTotalCharge(Long.valueOf(bankChargeRuleDO.getMaxFee()));
                    } else {
                        billPayDO.setPayTotalCharge(total * bankChargeRuleDO.getChargeUnit() / 10000);
                    }
                    billPayDAO.updateByIdSelective(billPayDO);
                }

                if (confirmPayForm.getDepositValList() != null && confirmPayForm.getDepositTypeList() != null) {
                    for (int i = 0, len = confirmPayForm.getDepositValList().size(); i < len; ++i) {
                        String val = confirmPayForm.getDepositValList().get(i);
                        Long typeId = confirmPayForm.getDepositTypeList().get(i);
                        BillExtDO billExtDO = new BillExtDO();
                        billExtDO.setBillPayId(payId);
                        billExtDO.setIsPayed(1);
                        billExtDO.setPayTotal(PriceUtil.convertPriceYuan(val));
                        SysVarDO sysVarById1 = sysVarDAO.getSysVarById(typeId);
                        billExtDO.setBillCat(BillCatEnum.deposit.getDesc());
                        billExtDO.setBillTypeId(typeId);
                        billExtDO.setBillType(sysVarById1.getVarValue());
                        billExtDO.setIsRefund(0);
                        billExtDO.setSuiteId(confirmPayForm.getSuiteId());
                        billExtDAO.insert(billExtDO);

                    }
                }
                if (confirmPayForm.getOtherValList() != null && confirmPayForm.getOtherTypeList() != null) {
                    for (int i = 0, len = confirmPayForm.getOtherValList().size(); i < len; ++i) {
                        String val = confirmPayForm.getOtherValList().get(i);
                        Long typeId = confirmPayForm.getOtherTypeList().get(i);
                        BillExtDO billExtDO = new BillExtDO();
                        billExtDO.setBillPayId(payId);
                        billExtDO.setIsPayed(1);
                        billExtDO.setPayTotal(PriceUtil.convertPriceYuan(val));
                        SysVarDO sysVarById1 = sysVarDAO.getSysVarById(typeId);
                        billExtDO.setBillCat(BillCatEnum.other.getDesc());
                        billExtDO.setBillTypeId(typeId);
                        billExtDO.setBillType(sysVarById1.getVarValue());
                        billExtDO.setIsRefund(0);
                        billExtDO.setSuiteId(confirmPayForm.getSuiteId());
                        billExtDAO.insert(billExtDO);
                    }
                }
                if (confirmPayForm.getIcValList() != null && confirmPayForm.getIcTypeList() != null) {
                    for (int i = 0, len = confirmPayForm.getIcValList().size(); i < len; ++i) {
                        String val = confirmPayForm.getIcValList().get(i);
                        String type = confirmPayForm.getIcTypeList().get(i);

                        if(IcTypeEnum.elec.name().equals(type)){
                            //IC卡电费
                            BillElecDO billElecDO = new BillElecDO();
                            billElecDO.setIsPayed(1);
                            billElecDO.setPayTotal(PriceUtil.convertPriceYuan(val));
                            billElecDO.setResetZero(0);
                            billElecDO.setSuiteId(confirmPayForm.getSuiteId());
                            billElecDO.setBillPayId(payId);
                            billElecDO.setDiscount(0L);
                            billElecDO.setDiscountSelf(0L);
                            billElecDO.setDiscountEstate(0L);
                            billElecDO.setGmtPay(new Date());
                            billElecDO.setGmtRecord(new Date());
                            billElecDO.setGmtStart(new Date());
                            billElecDO.setGmtEnd(new Date());
                            billElecDO.setRecordTotal(0);
                            billElecDO.setRecordLast(0);
                            billElecDO.setRecordCurrent(0);
                            Calendar calendar = Calendar.getInstance();
                            int year = calendar.get(Calendar.YEAR);
                            int month = calendar.get(Calendar.MONTH)+1;
                            billElecDO.setRecordYear(year);
                            billElecDO.setRecordMonth(month);
                            billElecDAO.insert(billElecDO);
                        }else{
                            //IC卡水费
                            BillWaterDO billWaterDO = new BillWaterDO();
                            billWaterDO.setIsPayed(1);
                            billWaterDO.setPayTotal(PriceUtil.convertPriceYuan(val));
                            billWaterDO.setResetZero(0);
                            billWaterDO.setSuiteId(confirmPayForm.getSuiteId());
                            billWaterDO.setBillPayId(payId);
                            billWaterDO.setDiscount(0L);
                            billWaterDO.setDiscountSelf(0L);
                            billWaterDO.setDiscountEstate(0L);
                            billWaterDO.setGmtPay(new Date());
                            billWaterDO.setGmtRecord(new Date());
                            billWaterDO.setGmtStart(new Date());
                            billWaterDO.setGmtEnd(new Date());
                            billWaterDO.setRecordTotal(0);
                            billWaterDO.setRecordLast(0);
                            billWaterDO.setRecordCurrent(0);
                            Calendar calendar = Calendar.getInstance();
                            int year = calendar.get(Calendar.YEAR);
                            int month = calendar.get(Calendar.MONTH)+1;
                            billWaterDO.setRecordYear(year);
                            billWaterDO.setRecordMonth(month);
                            billWaterDAO.insert(billWaterDO);
                        }

                    }
                }

                bizResult.data.put("total", total);
            }
        });
        bizResult.success = true;

        return bizResult;
    }

    @Override
    public BizResult initForPrint(UnpaySearchForm unpaySearchForm) {
        BizResult bizResult = new BizResult();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date());
        bizResult.data.put("printDay", format);
        OwnerQuery ownerQuery = new OwnerQuery();
        ownerQuery.setSuiteId(unpaySearchForm.getSuiteId());
        List<OwnerDO> ownerDOs = ownerDAO.selectForPage(ownerQuery);
        bizResult.data.put("ownerDOList", ownerDOs);

        List<BillParkingDO> billParkingDOList = new ArrayList<BillParkingDO>();
        List<BillEstateDO> billEstateDOList = new ArrayList<BillEstateDO>();
        List<BillWaterDO> billWaterDOList = new ArrayList<BillWaterDO>();
        List<BillElecDO> billElecDOList = new ArrayList<BillElecDO>();
        long total = 0L;
        if (unpaySearchForm.getBillIdList() != null) {
            List<String> billIdList = unpaySearchForm.getBillIdList();
            for (String billId : billIdList) {
                String[] split = billId.split("_");
                if ("parking".equals(split[0])) {
                    BillParkingDO billParkingById = billParkingDAO.getBillParkingById(Convert.toLong(split[1]));
                    total = total + billParkingById.getPayTotal();
                    billParkingDOList.add(billParkingById);
                } else if ("estate".equals(split[0])) {
                    BillEstateDO billEstateById = billEstateDAO.getBillEstateById(Convert.toLong(split[1]));
                    total = total + billEstateById.getPayTotal();
                    billEstateDOList.add(billEstateById);
                } else if ("water".equals(split[0])) {
                    BillWaterDO billWaterById = billWaterDAO.getBillWaterById(Convert.toLong(split[1]));
                    total = total + billWaterById.getPayTotal();
                    billWaterDOList.add(billWaterById);
                } else if ("elec".equals(split[0])) {
                    BillElecDO billElecById = billElecDAO.getBillElecById(Convert.toLong(split[1]));
                    total = total + billElecById.getPayTotal();
                    billElecDOList.add(billElecById);
                }
            }
        }
        //计算车位费
        long allTotal = 0L;
        if (billParkingDOList.size() > 0) {
            Collections.sort(billParkingDOList, new Comparator<BillParkingDO>() {
                @Override
                public int compare(BillParkingDO o1, BillParkingDO o2) {
                    return o2.getGmtStart().compareTo(o1.getGmtStart());
                }
            });
            BillParkingDO billParkingDO = billParkingDOList.get(0);
            BillParkingVO billParkingVO = new BillParkingVO();
            billParkingVO.setBillParkingDO(billParkingDO);

            ParkingDO parkingById = parkingDAO.getParkingById(billParkingDO.getParkingId());
            billParkingVO.setParkingDO(parkingById);
            if (parkingById.getBuyed().intValue() == 0) {
                ParkingRentRuleQuery parkingRentRuleQuery = new ParkingRentRuleQuery();
                parkingRentRuleQuery.setVarId(parkingById.getParkingTypeId());
                parkingRentRuleQuery.setVillageId(unpaySearchForm.getVillageId());
                List<ParkingRentRuleDO> parkingRentRuleDOs = parkingRentRuleDAO.selectForView(parkingRentRuleQuery);
                billParkingVO.setParkingFee(parkingRentRuleDOs.get(0).getFeeUnit());
            } else {
                ParkingRuleQuery parkingRuleQuery = new ParkingRuleQuery();
                parkingRuleQuery.setVarId(parkingById.getParkingTypeId());
                parkingRuleQuery.setVillageId(unpaySearchForm.getVillageId());
                List<ParkingRuleDO> parkingRuleDOs = parkingRuleDAO.selectForView(parkingRuleQuery);
                billParkingVO.setParkingFee(parkingRuleDOs.get(0).getFeeUnit());
            }
            bizResult.data.put("billParkingVO", billParkingVO);
            long c = 0L;
            for (BillParkingDO billParkingDO1 : billParkingDOList) {
                c = c + billParkingDO1.getPayTotal();
            }
            billParkingVO.setTotalFee(c);
            bizResult.data.put("billParkingVO", billParkingVO);
            allTotal = allTotal + billParkingVO.getTotalFee();
        }
        //计算物业费
        if (billEstateDOList.size() > 0) {
            Collections.sort(billEstateDOList, new Comparator<BillEstateDO>() {
                @Override
                public int compare(BillEstateDO o1, BillEstateDO o2) {
                    return o2.getGmtStart().compareTo(o1.getGmtStart());
                }
            });
            BillEstateVO billEstateVO = new BillEstateVO();
            billEstateVO.setBillEstateDO(billEstateDOList.get(0));
            EstateRuleQuery estateRuleQuery = new EstateRuleQuery();
            //BuildingDO buildingById = buildingDAO.getBuildingById(unpaySearchForm.getBuildingId());

            long estateTotal = 0L;
            for (BillEstateDO billEstateDO : billEstateDOList) {
                estateTotal = estateTotal + billEstateDO.getPayTotal();
            }
            billEstateVO.setTotalFee(estateTotal);
            SuiteDO suiteDO = suiteDAO.getSuiteById(billEstateDOList.get(0).getSuiteId());

            estateRuleQuery.setVillageId(unpaySearchForm.getVillageId());
            estateRuleQuery.setVarId(suiteDO.getBuildingTypeId());
            List<EstateRuleDO> estateRuleDOs = estateRuleDAO.selectForView(estateRuleQuery);
            billEstateVO.setEstateRuleDO(estateRuleDOs.get(0));

            billEstateVO.setSuiteDO(suiteDO);
            billEstateVO.setMonthFee(suiteDO.getSuiteArea() * estateRuleDOs.get(0).getFeeUnit() / 1000);
            bizResult.data.put("billEstateVO", billEstateVO);
            allTotal = allTotal + billEstateVO.getTotalFee();
        }
        //水费
        if (billWaterDOList.size() > 0) {
            Collections.sort(billWaterDOList, new Comparator<BillWaterDO>() {
                @Override
                public int compare(BillWaterDO o1, BillWaterDO o2) {
                    return o2.getGmtStart().compareTo(o1.getGmtStart());
                }
            });
            BillWaterVO billWaterVO = new BillWaterVO();
            billWaterVO.setBillWaterDO(billWaterDOList.get(0));
            WaterRuleQuery waterRuleQuery = new WaterRuleQuery();
            waterRuleQuery.setVillageId(unpaySearchForm.getVillageId());
            List<WaterRuleDO> waterRuleDOs = waterRuleDAO.selectForView(waterRuleQuery);
            Collections.sort(waterRuleDOs, new Comparator<WaterRuleDO>() {
                @Override
                public int compare(WaterRuleDO o1, WaterRuleDO o2) {
                    return o1.getMinValue() - o2.getMinValue();
                }
            });
            billWaterVO.setWaterRuleDOList(waterRuleDOs);
            long totalWater = 0L;
            for (BillWaterDO billWaterDO : billWaterDOList) {
                totalWater = totalWater + billWaterDO.getPayTotal();
            }
            billWaterVO.setTotalFee(totalWater);
            billWaterVO.setNeedBefore(totalWater - billWaterDOList.get(0).getPayTotal());
            bizResult.data.put("billWaterVO", billWaterVO);
            allTotal = allTotal + billWaterVO.getTotalFee();
        }
        //电费
        if (billElecDOList.size() > 0) {
            Collections.sort(billElecDOList, new Comparator<BillElecDO>() {
                @Override
                public int compare(BillElecDO o1, BillElecDO o2) {
                    return o2.getGmtStart().compareTo(o1.getGmtStart());
                }
            });
            BillElecVO billElecVO = new BillElecVO();
            ElecRuleQuery elecRuleQuery = new ElecRuleQuery();
            elecRuleQuery.setVillageId(unpaySearchForm.getVillageId());
            List<ElecRuleDO> elecRuleDOs = elecRuleDAO.selectForView(elecRuleQuery);
            Collections.sort(elecRuleDOs, new Comparator<ElecRuleDO>() {
                @Override
                public int compare(ElecRuleDO o1, ElecRuleDO o2) {
                    return o1.getMinValue() - o2.getMinValue();
                }
            });
            billElecVO.setElecRuleDOList(elecRuleDOs);
            long totalElec = 0L;
            for (BillElecDO billElecDO : billElecDOList) {
                totalElec = totalElec + billElecDO.getPayTotal();
            }
            billElecVO.setTotalFee(totalElec);
            billElecVO.setNeedBefore(totalElec - billElecDOList.get(0).getPayTotal());
            bizResult.data.put("billElecVO", billElecVO);
            allTotal = allTotal + billElecVO.getTotalFee();
        }

        bizResult.data.put("allTotal", allTotal);
        bizResult.data.put("allTotalCN", PriceUtil.toRMB(allTotal));

        bizResult.success = true;
        return bizResult;
    }


    private NoticeBillVO countNoticeBill(Long suiteId,Date start,Date end) {
        NoticeBillVO noticeBillVO = new NoticeBillVO();
        SuiteDO suiteById = suiteDAO.getSuiteById(suiteId);
        noticeBillVO.setSuiteDO(suiteById);
        UnitDO unitById = unitDAO.getUnitById(suiteById.getUnitId());
        BuildingDO buildingById1 = buildingDAO.getBuildingById(unitById.getBuildingId());
        VillageDO villageDO = villageDAO.getVillageById(buildingById1.getVillageId());
        noticeBillVO.setVillageDO(villageDO);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date());
        noticeBillVO.setPrintDay(format);

        OwnerQuery ownerQuery = new OwnerQuery();
        ownerQuery.setSuiteId(suiteId);
        List<OwnerDO> ownerDOs = ownerDAO.selectForPage(ownerQuery);
        noticeBillVO.setOwnerDOList(ownerDOs);

        BillParkingQuery billParkingQuery = new BillParkingQuery();
        billParkingQuery.setPageSize(500);
        billParkingQuery.setSuiteId(suiteId);
        billParkingQuery.setEndDate(end);
        billParkingQuery.setStartDate(start);
        List<BillParkingDO> billParkingDOList = billParkingDAO.selectForUnpay(billParkingQuery);

        BillEstateQuery billEstateQuery = new BillEstateQuery();
        billEstateQuery.setPageSize(500);
        billEstateQuery.setSuiteId(suiteId);
        billEstateQuery.setEndDate(end);
        billEstateQuery.setStartDate(start);
        List<BillEstateDO> billEstateDOList = billEstateDAO.selectForUnpay(billEstateQuery);

        BillWaterQuery billWaterQuery = new BillWaterQuery();
        billWaterQuery.setPageSize(500);
        billWaterQuery.setSuiteId(suiteId);
        billWaterQuery.setEndDate(end);
        billWaterQuery.setStartDate(start);
        List<BillWaterDO> billWaterDOList = billWaterDAO.selectForUnpay(billWaterQuery);

        BillElecQuery billElecQuery = new BillElecQuery();
        billElecQuery.setPageSize(500);
        billElecQuery.setSuiteId(suiteId);
        billElecQuery.setEndDate(end);
        billElecQuery.setStartDate(start);
        List<BillElecDO> billElecDOList = billElecDAO.selectForUnpay(billElecQuery);

        //计算车位费
        long allTotal = 0L;
        if (billParkingDOList.size() > 0) {
            Collections.sort(billParkingDOList, new Comparator<BillParkingDO>() {
                @Override
                public int compare(BillParkingDO o1, BillParkingDO o2) {
                    return o2.getGmtStart().compareTo(o1.getGmtStart());
                }
            });
            BillParkingDO billParkingDO = billParkingDOList.get(0);
            BillParkingVO billParkingVO = new BillParkingVO();
            billParkingVO.setBillParkingDO(billParkingDO);
            billParkingVO.setGmtStart(billParkingDOList.get(billParkingDOList.size() - 1).getGmtEnd());
            billParkingVO.setGmtEnd(billParkingDOList.get(0).getGmtStart());
            ParkingDO parkingById = parkingDAO.getParkingById(billParkingDO.getParkingId());
            billParkingVO.setParkingDO(parkingById);
            if (parkingById.getBuyed().intValue() == 0) {
                ParkingRentRuleQuery parkingRentRuleQuery = new ParkingRentRuleQuery();
                parkingRentRuleQuery.setVarId(parkingById.getParkingTypeId());
                parkingRentRuleQuery.setVillageId(villageDO.getVillageId());
                List<ParkingRentRuleDO> parkingRentRuleDOs = parkingRentRuleDAO.selectForView(parkingRentRuleQuery);
                billParkingVO.setParkingFee(parkingRentRuleDOs.get(0).getFeeUnit());
            } else {
                ParkingRuleQuery parkingRuleQuery = new ParkingRuleQuery();
                parkingRuleQuery.setVarId(parkingById.getParkingTypeId());
                parkingRuleQuery.setVillageId(villageDO.getVillageId());
                List<ParkingRuleDO> parkingRuleDOs = parkingRuleDAO.selectForView(parkingRuleQuery);
                billParkingVO.setParkingFee(parkingRuleDOs.get(0).getFeeUnit());
            }
            long c = 0L;
            for (BillParkingDO billParkingDO1 : billParkingDOList) {
                c = c + billParkingDO1.getPayTotal();
            }
            billParkingVO.setTotalFee(c);
            noticeBillVO.setBillParkingVO(billParkingVO);
            allTotal = allTotal + billParkingVO.getTotalFee();
        }
        //计算物业费
        if (billEstateDOList.size() > 0) {
            Collections.sort(billEstateDOList, new Comparator<BillEstateDO>() {
                @Override
                public int compare(BillEstateDO o1, BillEstateDO o2) {
                    return o2.getGmtStart().compareTo(o1.getGmtStart());
                }
            });
            BillEstateVO billEstateVO = new BillEstateVO();
            billEstateVO.setGmtStart(billEstateDOList.get(billEstateDOList.size() - 1).getGmtEnd());
            billEstateVO.setGmtEnd(billEstateDOList.get(0).getGmtStart());
            billEstateVO.setBillEstateDO(billEstateDOList.get(0));
            EstateRuleQuery estateRuleQuery = new EstateRuleQuery();
            estateRuleQuery.setVillageId(villageDO.getVillageId());
            estateRuleQuery.setVarId(suiteById.getBuildingTypeId());
            List<EstateRuleDO> estateRuleDOs = estateRuleDAO.selectForView(estateRuleQuery);
            billEstateVO.setEstateRuleDO(estateRuleDOs.get(0));
            long estateTotal = 0L;
            for (BillEstateDO billEstateDO : billEstateDOList) {
                estateTotal = estateTotal + billEstateDO.getPayTotal();
            }
            billEstateVO.setTotalFee(estateTotal);
            SuiteDO suiteDO = suiteDAO.getSuiteById(billEstateDOList.get(0).getSuiteId());
            billEstateVO.setSuiteDO(suiteDO);
            billEstateVO.setMonthFee(suiteDO.getSuiteArea() * estateRuleDOs.get(0).getFeeUnit() / 1000);
            noticeBillVO.setBillEstateVO(billEstateVO);
            allTotal = allTotal + billEstateVO.getTotalFee();
        }
        //水费
        if (billWaterDOList.size() > 0) {
            Collections.sort(billWaterDOList, new Comparator<BillWaterDO>() {
                @Override
                public int compare(BillWaterDO o1, BillWaterDO o2) {
                    return o2.getGmtStart().compareTo(o1.getGmtStart());
                }
            });
            BillWaterVO billWaterVO = new BillWaterVO();
            billWaterVO.setBillWaterDOList(billWaterDOList);
            WaterRuleQuery waterRuleQuery = new WaterRuleQuery();
            waterRuleQuery.setVillageId(villageDO.getVillageId());
            waterRuleQuery.setBuildingTypeId(suiteById.getBuildingTypeId());
            List<WaterRuleDO> waterRuleDOs = waterRuleDAO.selectForView(waterRuleQuery);
            Collections.sort(waterRuleDOs, new Comparator<WaterRuleDO>() {
                @Override
                public int compare(WaterRuleDO o1, WaterRuleDO o2) {
                    return o1.getMinValue() - o2.getMinValue();
                }
            });
            billWaterVO.setWaterRuleDOList(waterRuleDOs);
            noticeBillVO.setBillWaterVO(billWaterVO);
            allTotal = allTotal + billWaterVO.getTotalFee();
        }
        //电费
        if (billElecDOList.size() > 0) {
            Collections.sort(billElecDOList, new Comparator<BillElecDO>() {
                @Override
                public int compare(BillElecDO o1, BillElecDO o2) {
                    return o2.getGmtStart().compareTo(o1.getGmtStart());
                }
            });
            BillElecVO billElecVO = new BillElecVO();
            billElecVO.setBillElecDOList(billElecDOList);
            ElecRuleQuery elecRuleQuery = new ElecRuleQuery();
            elecRuleQuery.setVillageId(villageDO.getVillageId());
            elecRuleQuery.setBuildingTypeId(suiteById.getBuildingTypeId());
            List<ElecRuleDO> elecRuleDOs = elecRuleDAO.selectForView(elecRuleQuery);
            Collections.sort(elecRuleDOs, new Comparator<ElecRuleDO>() {
                @Override
                public int compare(ElecRuleDO o1, ElecRuleDO o2) {
                    return o1.getMinValue() - o2.getMinValue();
                }
            });
            billElecVO.setElecRuleDOList(elecRuleDOs);

            noticeBillVO.setBillElecVO(billElecVO);
            allTotal = allTotal + billElecVO.getTotalFee();
        }
        noticeBillVO.setAllTotal(allTotal);
        noticeBillVO.setAllTotalCN(PriceUtil.toRMB(allTotal));
        return noticeBillVO;
    }

    @Override
    public BizResult cancelBill(final String id) {
        BizResult bizResult = new BizResult();
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
                String[] split = id.split("_");
                SysVarQuery sysVarQuery = new SysVarQuery();
                sysVarQuery.setVarType(SysVarTypeEnum.bank_charge.name());

                List<SysVarDO> payTypeSysVarDOs = sysVarDAO.selectForPage(sysVarQuery);

                long suiteId=0;
                if ("parking".equals(split[0])) {
                    BillParkingDO billParkingById = billParkingDAO.getBillParkingById(Convert.toLong(split[1]));
                    BillParkingDO temp = new BillParkingDO();
                    temp.setIsPayed(2);
                    temp.setBillId(billParkingById.getBillId());
                    billParkingDAO.updateByIdSelective(temp);
                    suiteId = billParkingById.getSuiteId();

                } else if ("estate".equals(split[0])) {
                    BillEstateDO billEstateById = billEstateDAO.getBillEstateById(Convert.toLong(split[1]));
                    BillEstateDO temp = new BillEstateDO();
                    temp.setIsPayed(2);
                    temp.setBillId(billEstateById.getBillId());
                    billEstateDAO.updateByIdSelective(temp);
                    suiteId = billEstateById.getSuiteId();
                } else if ("water".equals(split[0])) {
                    BillWaterDO billWaterById = billWaterDAO.getBillWaterById(Convert.toLong(split[1]));
                    BillWaterDO temp = new BillWaterDO();
                    temp.setIsPayed(2);
                    temp.setBillId(billWaterById.getBillId());
                    billWaterDAO.updateByIdSelective(temp);
                    suiteId = billWaterById.getSuiteId();
                } else if ("elec".equals(split[0])) {
                    BillElecDO billElecById = billElecDAO.getBillElecById(Convert.toLong(split[1]));
                    BillElecDO temp = new BillElecDO();
                    temp.setIsPayed(2);
                    temp.setBillId(billElecById.getBillId());
                    billElecDAO.updateByIdSelective(temp);
                    suiteId = billElecById.getSuiteId();
                } else if ("deposit".equals(split[0])) {
                    BillExtDO billExtById = billExtDAO.getBillExtById(Convert.toLong(split[1]));
                    BillExtDO temp = new BillExtDO();
                    temp.setIsPayed(2);
                    temp.setBillId(billExtById.getBillId());
                    billExtDAO.updateByIdSelective(temp);
                    suiteId = billExtById.getSuiteId();
                } else if ("other".equals(split[0])) {
                    BillExtDO billExtById = billExtDAO.getBillExtById(Convert.toLong(split[1]));
                    BillExtDO temp = new BillExtDO();
                    temp.setIsPayed(2);
                    temp.setBillId(billExtById.getBillId());
                    billExtDAO.updateByIdSelective(temp);
                    suiteId = billExtById.getSuiteId();
                }
                BillPayDO billPayDO = new BillPayDO();
                billPayDO.setSuiteId(suiteId);
                billPayDO.setPayTypeId(payTypeSysVarDOs.get(0).getVarId());
                billPayDO.setPayType(payTypeSysVarDOs.get(0).getVarValue());
                billPayDO.setPayComment("账单作废");
                billPayDO.setPayTotal(0L);
                billPayDO.setPayDesc("账单作废");
                billPayDO.setPayTotalCharge(0L);
                billPayDAO.insert(billPayDO);
            }
        });
        bizResult.success = true;
        return bizResult;
    }

    @Override
    public BizResult queryForReverse(long payId) {
        BizResult bizResult = new BizResult();
        BillPayDO billPayById = billPayDAO.getBillPayById(payId);
        if (billPayById == null) {
            bizResult.code = CommonResultCode.RECORD_NOT_FOUND;
            return bizResult;
        }
        List<BillParkingDO> billParkingDOList = billParkingDAO.getByPayId(payId);
        List<BillExtDO> billExtDOList = billExtDAO.getByPayId(payId);
        List<BillElecDO> billElecDOList = billElecDAO.getByPayId(payId);
        List<BillWaterDO> billWaterDOList = billWaterDAO.getByPayId(payId);
        List<BillEstateDO> billEstateDOList = billEstateDAO.getByPayId(payId);

        bizResult.data.put("billParkingList", billParkingDOList);
        bizResult.data.put("billExtList", billExtDOList);
        bizResult.data.put("billElecList", billElecDOList);
        bizResult.data.put("billWaterList", billWaterDOList);
        bizResult.data.put("billEstateList", billEstateDOList);
        bizResult.success = true;
        return bizResult;
    }

    @Override
    public BizResult reverse(final ReverseForm reverseForm) {
        BizResult bizResult = new BizResult();

        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
                BillPayDO billPayById = billPayDAO.getBillPayById(reverseForm.getPayId());
                if (billPayById == null) {
                    return;
                }


                List<BillParkingDO> billParkingDOList = billParkingDAO.getByPayId(reverseForm.getPayId());
                for (BillParkingDO billParkingDO : billParkingDOList) {
                    BillParkingDO temp = new BillParkingDO();
                    temp.setBillPayId(billParkingDO.getBillPayId());
                    temp.setIsPayed(BooleanEnum.FALSE.getVal());
                    temp.setDiscount(0L);
                    temp.setDiscountSelf(0L);
                    temp.setDiscountEstate(0L);
                    temp.setBillPayId(0L);
                    //找回原价
                    temp.setPayTotal(getOriginPrice(billParkingDO.getPayTotal(),billParkingDO.getDiscountEstate(),billParkingDO.getDiscountSelf(),
                            billParkingDO.getDiscount()));
                    billParkingDAO.updateByIdSelective(temp);
                }
                List<BillExtDO> billExtDOList = billExtDAO.getByPayId(reverseForm.getPayId());
                for (BillExtDO billExtDO : billExtDOList) {
                    BillExtDO temp = new BillExtDO();
                    temp.setBillId(billExtDO.getBillId());
                    temp.setIsPayed(BooleanEnum.FALSE.getVal());
                    temp.setDiscount(0L);
                    temp.setDiscountSelf(0L);
                    temp.setDiscountEstate(0L);
                    temp.setBillPayId(0L);
                    //找回原价
                    temp.setPayTotal(getOriginPrice(billExtDO.getPayTotal(), billExtDO.getDiscountEstate(), billExtDO.getDiscountSelf(),
                            billExtDO.getDiscount()));
                    billExtDAO.updateByIdSelective(temp);
                }
                List<BillElecDO> billElecDOList = billElecDAO.getByPayId(reverseForm.getPayId());
                for (BillElecDO billElecDO : billElecDOList) {
                    BillElecDO temp = new BillElecDO();
                    temp.setBillId(billElecDO.getBillId());
                    temp.setIsPayed(BooleanEnum.FALSE.getVal());
                    temp.setDiscount(0L);
                    temp.setDiscountSelf(0L);
                    temp.setDiscountEstate(0L);
                    temp.setBillPayId(0L);
                    //找回原价
                    temp.setPayTotal(getOriginPrice(billElecDO.getPayTotal(), billElecDO.getDiscountEstate(), billElecDO.getDiscountSelf(),
                            billElecDO.getDiscount()));
                    billElecDAO.updateByIdSelective(temp);
                }
                List<BillWaterDO> billWaterDOList = billWaterDAO.getByPayId(reverseForm.getPayId());
                for (BillWaterDO billWaterDO : billWaterDOList) {
                    BillWaterDO temp = new BillWaterDO();
                    temp.setBillId(billWaterDO.getBillId());
                    temp.setIsPayed(BooleanEnum.FALSE.getVal());
                    temp.setDiscount(0L);
                    temp.setDiscountSelf(0L);
                    temp.setDiscountEstate(0L);
                    temp.setBillPayId(0L);
                    //找回原价
                    temp.setPayTotal(getOriginPrice(billWaterDO.getPayTotal(), billWaterDO.getDiscountEstate(), billWaterDO.getDiscountSelf(),
                            billWaterDO.getDiscount()));
                    billWaterDAO.updateByIdSelective(temp);
                }
                List<BillEstateDO> billEstateDOList = billEstateDAO.getByPayId(reverseForm.getPayId());
                for (BillEstateDO billEstateDO : billEstateDOList) {
                    BillEstateDO temp = new BillEstateDO();
                    temp.setBillId(billEstateDO.getBillId());
                    temp.setIsPayed(BooleanEnum.FALSE.getVal());
                    temp.setDiscount(0L);
                    temp.setDiscountSelf(0L);
                    temp.setDiscountEstate(0L);
                    temp.setBillPayId(0L);
                    //找回原价
                    temp.setPayTotal(getOriginPrice(billEstateDO.getPayTotal(), billEstateDO.getDiscountEstate(), billEstateDO.getDiscountSelf(),
                            billEstateDO.getDiscount()));
                    billEstateDAO.updateByIdSelective(temp);
                }
                //如果开发票了，发票也需要自动冲抵掉
                if (billPayById.getInvoiceId() != null && billPayById.getInvoiceId().longValue() > 0) {
                    InvoiceLogDO invoiceLogById = invoiceLogDAO.getInvoiceLogById(billPayById.getInvoiceId());
                    InvoiceLogDO invoiceLogDO = new InvoiceLogDO();
                    invoiceLogDO.setLogId(invoiceLogById.getLogId());
                    invoiceLogDO.setOffsetStatus(BooleanEnum.TRUE.getVal());
                    invoiceLogDO.setOffsetComment("缴费冲抵，发票自动冲抵");
                    invoiceLogDAO.updateByIdSelective(invoiceLogDO);
                }

                BillPayDO billPayDO = new BillPayDO();
                billPayDO.setPayId(reverseForm.getPayId());
                billPayDO.setOffsetStatus(BooleanEnum.TRUE.getVal());
                billPayDO.setOffsetComment(reverseForm.getOffsetComment());
                billPayDO.setInvoiceId(0L);
                billPayDAO.updateByIdSelective(billPayDO);
            }
        });
        bizResult.success = true;
        return bizResult;
    }

    private long getOriginPrice(Long payTotal,Long dichan,Long self,Long discount){
        if(payTotal == null ){
            return 0;
        }
        if(discount!=null && discount >0){
            payTotal = payTotal*10000/discount;
        }
        if(dichan!=null && dichan >0){
            payTotal+=dichan;
        }
        if(self!=null && self > 0){
            payTotal+=self;
        }
        return payTotal;
    }

    @Override
    public BizResult initForPrintBatch(BatchNoticeBillForm batchNoticeBillForm) {
        BizResult bizResult = new BizResult();

        Date startDate = null;
        Date endDate = null;
        if(StringUtils.hasText(batchNoticeBillForm.getStartDate())&&
                StringUtils.hasText(batchNoticeBillForm.getEndDate())){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");

            try {
                startDate = simpleDateFormat.parse(batchNoticeBillForm.getStartDate());
                simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
                endDate = simpleDateFormat.parse(batchNoticeBillForm.getEndDate());
                if (startDate.after(endDate)) {
                    Date temp = startDate;
                    startDate = endDate;
                    endDate = temp;
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        List<NoticeBillVO> noticeBillVOList = new ArrayList<NoticeBillVO>();
        List<Long> suiteIdList = getSuiteIdList(batchNoticeBillForm);

        for (Long id : suiteIdList) {
            NoticeBillVO noticeBillVO = countNoticeBill(id,startDate,endDate);
            noticeBillVOList.add(noticeBillVO);
        }
        bizResult.data.put("noticeBillVOList", noticeBillVOList);

        if (batchNoticeBillForm.getVillageId() != null) {
            BuildingQuery buildingQuery = new BuildingQuery();
            buildingQuery.setPageSize(10000);
            buildingQuery.setVillageId(batchNoticeBillForm.getVillageId());
            List<BuildingDO> buildingDOs = buildingDAO.selectForPage(buildingQuery);
            bizResult.data.put("buildingList", buildingDOs);
            if (batchNoticeBillForm.getBuildingId() == null) {
                bizResult.success = true;
                return bizResult;
            }
        }
        if (batchNoticeBillForm.getBuildingId() != null) {
            UnitQuery unitQuery = new UnitQuery();
            unitQuery.setPageSize(10000);
            unitQuery.setBuildingId(batchNoticeBillForm.getBuildingId());
            List<UnitDO> unitDOs = unitDAO.selectForPage(unitQuery);
            bizResult.data.put("unitList", unitDOs);
        }

        if (batchNoticeBillForm.getUnitId() != null) {
            SuiteQuery suiteQuery = new SuiteQuery();
            suiteQuery.setPageSize(10000);
            suiteQuery.setUnitId(batchNoticeBillForm.getUnitId());
            List<SuiteDO> suiteDOs = suiteDAO.selectForPage(suiteQuery);
            bizResult.data.put("suiteList", suiteDOs);
        }

        bizResult.success = true;
        return bizResult;
    }

    @Override
    public BizResult statUnpay(UnpayQuery unpayQuery) {
        BizResult bizResult = new BizResult();
        List<Long> suiteIdList = getSuiteIdList(unpayQuery);
        List<UnpayVO> unpayVOList = new ArrayList<UnpayVO>();
        for(Long suiteId:suiteIdList) {
            UnpayVO unpayVO = new UnpayVO();
            SuiteDO suiteById = suiteDAO.getSuiteById(suiteId);
            unpayVO.setSuiteDO(suiteById);
            UnitDO unitById = unitDAO.getUnitById(suiteById.getUnitId());
            unpayVO.setUnitDO(unitById);
            BuildingDO buildingById = buildingDAO.getBuildingById(unitById.getBuildingId());
            unpayVO.setBuildingDO(buildingById);
            VillageDO villageById = villageDAO.getVillageById(buildingById.getVillageId());
            unpayVO.setVillageDO(villageById);
            OwnerQuery ownerQuery = new OwnerQuery();
            ownerQuery.setSuiteId(suiteId);
            List<OwnerDO> ownerDOs = ownerDAO.selectForPage(ownerQuery);
            unpayVO.setOwnerDOList(ownerDOs);
            countBill(suiteId, unpayVO);
            unpayVOList.add(unpayVO);
        }
        bizResult.data.put("unpayVOList",unpayVOList);
        bizResult.success = true;
        return bizResult;
    }

    @Override
    public BizResult queryDetail(long id) {
        BizResult bizResult = new BizResult();
        List<BillEstateDO> billEstateDOList = billEstateDAO.getByPayId(id);
        List<BillParkingDO> billParkingDOList = billParkingDAO.getByPayId(id);
        List<BillElecDO> billElecDOList = billElecDAO.getByPayId(id);
        List<BillWaterDO> billWaterDOList = billWaterDAO.getByPayId(id);
        List<BillExtDO> billExtDOList = billExtDAO.getByPayId(id);
        long allTotal = 0L;
        PayDetailVO payDetailVO = new PayDetailVO();
        List<PayDetail> payDetailList = new ArrayList<PayDetail>();
        payDetailVO.setPayDetailList(payDetailList);
        if(billEstateDOList.size()>0){
            Collections.sort(billEstateDOList, new Comparator<BillEstateDO>() {
                @Override
                public int compare(BillEstateDO o1, BillEstateDO o2) {
                    return o2.getGmtStart().compareTo(o1.getGmtStart());
                }
            });
            BillEstateDO billEstateDO = billEstateDOList.get(0);
            if(payDetailVO.getVillageDO() == null) {
                SuiteDO suiteById = suiteDAO.getSuiteById(billEstateDO.getSuiteId());
                payDetailVO.setSuiteDO(suiteById);
                UnitDO unitById = unitDAO.getUnitById(suiteById.getUnitId());
                BuildingDO buildingById = buildingDAO.getBuildingById(unitById.getBuildingId());
                payDetailVO.setVillageDO(villageDAO.getVillageById(buildingById.getVillageId()));
                OwnerQuery ownerQuery = new OwnerQuery();
                ownerQuery.setSuiteId(suiteById.getSuiteId());
                List<OwnerDO> ownerDOs = ownerDAO.selectForPage(ownerQuery);
                payDetailVO.setOwnerDOList(ownerDOs);
                payDetailVO.setGmt(billEstateDO.getGmtCreate());
            }
            PayDetail payDetail = new PayDetail();
            payDetail.setFeeName("物业费");
            payDetail.setStart(billEstateDOList.get(billEstateDOList.size() - 1).getGmtStart());
            payDetail.setEnd(billEstateDOList.get(0).getGmtEnd());

            long c = 0L;
            for (BillEstateDO billEstateDO1 : billEstateDOList) {
                c = c + billEstateDO1.getPayTotal();
            }
            payDetail.setPayTotal(c);
            payDetailList.add(payDetail);
            allTotal = allTotal + payDetail.getPayTotal();
        }

        if(billWaterDOList.size()>0){
            Collections.sort(billWaterDOList, new Comparator<BillWaterDO>() {
                @Override
                public int compare(BillWaterDO o1, BillWaterDO o2) {
                    return o2.getGmtStart().compareTo(o1.getGmtStart());
                }
            });
            BillWaterDO billWaterDO = billWaterDOList.get(0);
            if(payDetailVO.getVillageDO() == null) {
                SuiteDO suiteById = suiteDAO.getSuiteById(billWaterDO.getSuiteId());
                payDetailVO.setSuiteDO(suiteById);
                UnitDO unitById = unitDAO.getUnitById(suiteById.getUnitId());
                BuildingDO buildingById = buildingDAO.getBuildingById(unitById.getBuildingId());
                payDetailVO.setVillageDO(villageDAO.getVillageById(buildingById.getVillageId()));
                OwnerQuery ownerQuery = new OwnerQuery();
                ownerQuery.setSuiteId(suiteById.getSuiteId());
                List<OwnerDO> ownerDOs = ownerDAO.selectForPage(ownerQuery);
                payDetailVO.setOwnerDOList(ownerDOs);
                payDetailVO.setGmt(billWaterDO.getGmtCreate());
            }
            PayDetail payDetail = new PayDetail();
            payDetail.setFeeName("水费");
            payDetail.setStart(billWaterDOList.get(billWaterDOList.size() - 1).getGmtStart());
            payDetail.setEnd(billWaterDOList.get(0).getGmtEnd());

            long c = 0L;
            for (BillWaterDO billWaterDO1 : billWaterDOList) {
                c = c + billWaterDO1.getPayTotal();
            }
            payDetail.setPayTotal(c);
            payDetailList.add(payDetail);
            allTotal = allTotal + payDetail.getPayTotal();
        }

        if(billElecDOList.size()>0){
            Collections.sort(billElecDOList, new Comparator<BillElecDO>() {
                @Override
                public int compare(BillElecDO o1, BillElecDO o2) {
                    return o2.getGmtStart().compareTo(o1.getGmtStart());
                }
            });
            BillElecDO billElecDO = billElecDOList.get(0);
            if(payDetailVO.getVillageDO() == null) {
                SuiteDO suiteById = suiteDAO.getSuiteById(billElecDO.getSuiteId());
                payDetailVO.setSuiteDO(suiteById);
                UnitDO unitById = unitDAO.getUnitById(suiteById.getUnitId());
                BuildingDO buildingById = buildingDAO.getBuildingById(unitById.getBuildingId());
                payDetailVO.setVillageDO(villageDAO.getVillageById(buildingById.getVillageId()));
                OwnerQuery ownerQuery = new OwnerQuery();
                ownerQuery.setSuiteId(suiteById.getSuiteId());
                List<OwnerDO> ownerDOs = ownerDAO.selectForPage(ownerQuery);
                payDetailVO.setOwnerDOList(ownerDOs);
                payDetailVO.setGmt(billElecDO.getGmtCreate());
            }
            PayDetail payDetail = new PayDetail();
            payDetail.setFeeName("电费");

            payDetail.setStart(billElecDOList.get(billElecDOList.size() - 1).getGmtStart());
            payDetail.setEnd(billElecDOList.get(0).getGmtEnd());

            long c = 0L;
            for (BillElecDO billElecDO1 : billElecDOList) {
                c = c + billElecDO1.getPayTotal();
            }
            payDetail.setPayTotal(c);
            payDetailList.add(payDetail);
            allTotal = allTotal + payDetail.getPayTotal();
        }
        if (billParkingDOList.size() > 0) {
            Collections.sort(billParkingDOList, new Comparator<BillParkingDO>() {
                @Override
                public int compare(BillParkingDO o1, BillParkingDO o2) {
                    return o2.getGmtStart().compareTo(o1.getGmtStart());
                }
            });
            BillParkingDO billParkingDO = billParkingDOList.get(0);
            if(payDetailVO.getVillageDO() == null) {
                SuiteDO suiteById = suiteDAO.getSuiteById(billParkingDO.getSuiteId());
                payDetailVO.setSuiteDO(suiteById);
                UnitDO unitById = unitDAO.getUnitById(suiteById.getUnitId());
                BuildingDO buildingById = buildingDAO.getBuildingById(unitById.getBuildingId());
                payDetailVO.setVillageDO(villageDAO.getVillageById(buildingById.getVillageId()));
                OwnerQuery ownerQuery = new OwnerQuery();
                ownerQuery.setSuiteId(suiteById.getSuiteId());
                List<OwnerDO> ownerDOs = ownerDAO.selectForPage(ownerQuery);
                payDetailVO.setOwnerDOList(ownerDOs);
                payDetailVO.setGmt(billParkingDO.getGmtCreate());
            }

            PayDetail payDetail = new PayDetail();
            payDetail.setFeeName("车位费");
            payDetail.setStart(billParkingDOList.get(billParkingDOList.size() - 1).getGmtStart());
            payDetail.setEnd(billParkingDOList.get(0).getGmtEnd());

            long c = 0L;
            for (BillParkingDO billParkingDO1 : billParkingDOList) {
                c = c + billParkingDO1.getPayTotal();
            }
            payDetail.setPayTotal(c);
            payDetailList.add(payDetail);
            allTotal = allTotal + payDetail.getPayTotal();
        }
        if(billExtDOList.size()>0){
            List<BillExtDO> depositList = new ArrayList<BillExtDO>();
            List<BillExtDO> extList = new ArrayList<BillExtDO>();
            for(BillExtDO billExtDO:billExtDOList){
                if(billExtDO.getBillCat().equals(BillCatEnum.deposit.name())){
                    depositList.add(billExtDO);
                }else{
                    extList.add(billExtDO);
                }
            }
            if(depositList.size()>0){
                Collections.sort(depositList, new Comparator<BillExtDO>() {
                    @Override
                    public int compare(BillExtDO o1, BillExtDO o2) {
                        return o2.getGmtCreate().compareTo(o1.getGmtCreate());
                    }
                });
                BillExtDO billExtDO = depositList.get(0);
                if(payDetailVO.getVillageDO() == null) {
                    SuiteDO suiteById = suiteDAO.getSuiteById(billExtDO.getSuiteId());
                    payDetailVO.setSuiteDO(suiteById);
                    UnitDO unitById = unitDAO.getUnitById(suiteById.getUnitId());
                    BuildingDO buildingById = buildingDAO.getBuildingById(unitById.getBuildingId());
                    payDetailVO.setVillageDO(villageDAO.getVillageById(buildingById.getVillageId()));
                    OwnerQuery ownerQuery = new OwnerQuery();
                    ownerQuery.setSuiteId(suiteById.getSuiteId());
                    List<OwnerDO> ownerDOs = ownerDAO.selectForPage(ownerQuery);
                    payDetailVO.setOwnerDOList(ownerDOs);
                    payDetailVO.setGmt(billExtDO.getGmtCreate());
                }
                PayDetail payDetail = new PayDetail();
                payDetail.setFeeName("押金");
                payDetail.setStart(depositList.get(depositList.size() - 1).getGmtCreate());
                payDetail.setEnd(depositList.get(0).getGmtCreate());


                long c = 0L;
                for (BillExtDO billExtDO1 : depositList) {
                    c = c + billExtDO1.getPayTotal();
                }
                payDetail.setPayTotal(c);
                payDetailList.add(payDetail);
                allTotal = allTotal + payDetail.getPayTotal();
            }

            if(extList.size()>0){
                Collections.sort(extList, new Comparator<BillExtDO>() {
                    @Override
                    public int compare(BillExtDO o1, BillExtDO o2) {
                        return o2.getGmtCreate().compareTo(o1.getGmtCreate());
                    }
                });
                BillExtDO billExtDO = extList.get(0);
                if(payDetailVO.getVillageDO() == null) {
                    SuiteDO suiteById = suiteDAO.getSuiteById(billExtDO.getSuiteId());
                    payDetailVO.setSuiteDO(suiteById);
                    UnitDO unitById = unitDAO.getUnitById(suiteById.getUnitId());
                    BuildingDO buildingById = buildingDAO.getBuildingById(unitById.getBuildingId());
                    payDetailVO.setVillageDO(villageDAO.getVillageById(buildingById.getVillageId()));
                    OwnerQuery ownerQuery = new OwnerQuery();
                    ownerQuery.setSuiteId(suiteById.getSuiteId());
                    List<OwnerDO> ownerDOs = ownerDAO.selectForPage(ownerQuery);
                    payDetailVO.setOwnerDOList(ownerDOs);
                    payDetailVO.setGmt(billExtDO.getGmtCreate());
                }
                PayDetail payDetail = new PayDetail();
                payDetail.setFeeName("押金");
                payDetail.setStart(extList.get(extList.size() - 1).getGmtCreate());
                payDetail.setEnd(extList.get(0).getGmtCreate());

                long c = 0L;
                for (BillExtDO billExtDO1 : extList) {
                    c = c + billExtDO1.getPayTotal();
                }
                payDetail.setPayTotal(c);
                payDetailList.add(payDetail);
                allTotal = allTotal + payDetail.getPayTotal();
            }
        }
        payDetailVO.setPayTotal(allTotal);
        payDetailVO.setPayTotalCN(PriceUtil.toRMB(allTotal));
        bizResult.data.put("payDetailVO",payDetailVO);
        bizResult.success=true;

        return bizResult;
    }


    private void countBill(Long suiteId,UnpayVO unpayVO) {

        BillParkingQuery billParkingQuery = new BillParkingQuery();
        billParkingQuery.setPageSize(500);
        billParkingQuery.setSuiteId(suiteId);
        int i = billParkingDAO.countForUnpay(billParkingQuery);
        if(i>0){
            unpayVO.setParkingState(true);
        }

        BillEstateQuery billEstateQuery = new BillEstateQuery();
        billEstateQuery.setPageSize(500);
        billEstateQuery.setSuiteId(suiteId);
        i= billEstateDAO.countForUnpay(billEstateQuery);
        if(i>0){
            unpayVO.setEstateState(true);
        }
        BillWaterQuery billWaterQuery = new BillWaterQuery();
        billWaterQuery.setPageSize(500);
        billWaterQuery.setSuiteId(suiteId);
        i = billWaterDAO.countForUnpay(billWaterQuery);
        if(i>0){
            unpayVO.setWaterState(true);
        }

        BillElecQuery billElecQuery = new BillElecQuery();
        billElecQuery.setPageSize(500);
        billElecQuery.setSuiteId(suiteId);
        i = billElecDAO.countForUnpay(billElecQuery);
        if(i>0){
            unpayVO.setElecState(true);
        }

    }


    private List<Long> getSuiteIdList(BatchNoticeBillForm batchNoticeBillForm) {
        List<Long> suiteIdList = new ArrayList<Long>();
        if (batchNoticeBillForm.getSuiteId() != null) {
            suiteIdList.add(batchNoticeBillForm.getSuiteId());
            return suiteIdList;
        }
        if (batchNoticeBillForm.getUnitId() != null) {
            SuiteQuery suiteQuery = new SuiteQuery();
            suiteQuery.setUnitId(batchNoticeBillForm.getUnitId());
            suiteQuery.setPageSize(1000);
            List<SuiteDO> suiteDOs = suiteDAO.selectForPage(suiteQuery);
            suiteIdList = TransformHelper.transform(suiteDOs, new Transform<SuiteDO, Long>() {
                @Override
                public Long transform(SuiteDO object) {
                    return object.getSuiteId();
                }
            });
            return suiteIdList;
        }
        if (batchNoticeBillForm.getBuildingId() != null) {
            UnitQuery unitQuery = new UnitQuery();
            unitQuery.setPageSize(1000);
            unitQuery.setBuildingId(batchNoticeBillForm.getBuildingId());
            List<UnitDO> unitDOs = unitDAO.selectForPage(unitQuery);
            List<Long> unitIdList = TransformHelper.transform(unitDOs, new Transform<UnitDO, Long>() {
                @Override
                public Long transform(UnitDO object) {
                    return object.getUnitId();
                }
            });

            SuiteQuery suiteQuery = new SuiteQuery();
            suiteQuery.setUnitIdList(unitIdList);
            suiteQuery.setPageSize(1000);
            List<SuiteDO> suiteDOs = suiteDAO.selectForPage(suiteQuery);
            suiteIdList = TransformHelper.transform(suiteDOs, new Transform<SuiteDO, Long>() {
                @Override
                public Long transform(SuiteDO object) {
                    return object.getSuiteId();
                }
            });
            return suiteIdList;

        }
        if (batchNoticeBillForm.getVillageId() != null) {
            BuildingQuery buildingQuery = new BuildingQuery();
            buildingQuery.setPageSize(1000);
            buildingQuery.setVillageId(batchNoticeBillForm.getVillageId());
            List<BuildingDO> buildingDOs = buildingDAO.selectForPage(buildingQuery);
            List<Long> buildingIdList = TransformHelper.transform(buildingDOs, new Transform<BuildingDO, Long>() {
                @Override
                public Long transform(BuildingDO object) {
                    return object.getBuildingId();
                }
            });

            UnitQuery unitQuery = new UnitQuery();
            unitQuery.setPageSize(1000);
            unitQuery.setBuildingIdList(buildingIdList);
            List<UnitDO> unitDOs = unitDAO.selectForPage(unitQuery);
            List<Long> unitIdList = TransformHelper.transform(unitDOs, new Transform<UnitDO, Long>() {
                @Override
                public Long transform(UnitDO object) {
                    return object.getUnitId();
                }
            });

            SuiteQuery suiteQuery = new SuiteQuery();
            suiteQuery.setUnitIdList(unitIdList);
            suiteQuery.setPageSize(1000);
            List<SuiteDO> suiteDOs = suiteDAO.selectForPage(suiteQuery);
            suiteIdList = TransformHelper.transform(suiteDOs, new Transform<SuiteDO, Long>() {
                @Override
                public Long transform(SuiteDO object) {
                    return object.getSuiteId();
                }
            });
            return suiteIdList;
        }

        List<Long> villageIdList = AuthUser.getCurrentUser().getVillageIdList();
        BuildingQuery buildingQuery = new BuildingQuery();
        buildingQuery.setPageSize(1000);
        buildingQuery.setVillageIdList(villageIdList);
        List<BuildingDO> buildingDOs = buildingDAO.selectForPage(buildingQuery);
        List<Long> buildingIdList = TransformHelper.transform(buildingDOs, new Transform<BuildingDO, Long>() {
            @Override
            public Long transform(BuildingDO object) {
                return object.getBuildingId();
            }
        });

        UnitQuery unitQuery = new UnitQuery();
        unitQuery.setPageSize(1000);
        unitQuery.setBuildingIdList(buildingIdList);
        List<UnitDO> unitDOs = unitDAO.selectForPage(unitQuery);
        List<Long> unitIdList = TransformHelper.transform(unitDOs, new Transform<UnitDO, Long>() {
            @Override
            public Long transform(UnitDO object) {
                return object.getUnitId();
            }
        });

        SuiteQuery suiteQuery = new SuiteQuery();
        suiteQuery.setUnitIdList(unitIdList);
        suiteQuery.setPageSize(1000);
        List<SuiteDO> suiteDOs = suiteDAO.selectForPage(suiteQuery);
        suiteIdList = TransformHelper.transform(suiteDOs, new Transform<SuiteDO, Long>() {
            @Override
            public Long transform(SuiteDO object) {
                return object.getSuiteId();
            }
        });
        return suiteIdList;
    }


}
